Skip to main content

Difference between CMD, RUN and ENTRYPOINT in Docker

RUN is mainly used in the building process, like

RUN apt update && apt install xxxxx

CMD and ENTRYPOINT are used to run a certain command when an instance starts. The main difference is when a different command is provided when the instance starts:

CMD will be ignored and the provided command will be executed

ENTRYPOINT will be run first and then the provided command

Considering the following Docker setup with ENTRYPOINT:

FROM centos:7
RUN apt-get update
RUN apt-get -y install python
COPY ./opt/source code
ENTRYPOINT ["echo", "Hello"]
docker build -t Darwin .
docker run Darwin hostname
Hello 6e14beead430

notice the echo is executed before hostname

on the other hand with CMD:

FROM centos:7
RUN apt-get update
RUN apt-get -y install python
COPY ./opt/source code
CMD ["echo", "Hello"]
docker build -t Darwin .
docker run Darwin hostname
6e14beead430

NOTE: ENTRYPOINT can also be overwritten by use --entrypoint in the command.

Refs

1: Docker CMD vs. ENTRYPOINT: What’s the Difference and How to Choose, Zotero